﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ones.ServiceAccountManagement;
using System.Net.Mail;

namespace Ones.SysAdmin
{
    public partial class AccountDetail : System.Web.UI.Page
    {
        ServiceAccountManagementClient amc = new ServiceAccountManagementClient("accountBasicHttpBinding");
        ViewMembershipUserEmployee ad = new ViewMembershipUserEmployee();
        string userId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlRole.DataSource = amc.getRoles();
                ddlRole.DataTextField = "RoleName";
                ddlRole.DataValueField = "RoleName";
                ddlRole.DataBind();

                if (!string.IsNullOrEmpty(Session["UserId_gettingDetail"] as string))
                {
                    userId = Session["UserId_gettingDetail"].ToString();
                    ad = amc.getAccountDetails(userId);
                    txtEmpId.Text = ad.empId;
                    txtUserId.Value = ad.UserId.ToString();
                    txtFirstName.Text = ad.empFirstName;
                    txtLastName.Text = ad.empLastName;
                    txtUserName.Text = ad.UserName;
                    txtEmail.Text = ad.Email;
                    ddlGender.SelectedValue = (ad.empGender == true ? "male" : "female");
                    ddlRole.SelectedValue = ad.RoleName;
                    ddlState.SelectedValue = (ad.IsApproved == true ? "active" : "locked");
                }
                else
                {
                    Response.Redirect("Accounts.aspx");
                }
            }
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Accounts.aspx");
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            bool isApproved = false;
            bool isMale = false;
            if (ddlState.SelectedValue == "locked") isApproved = false;
            else if (ddlState.SelectedValue == "active") isApproved = true;
            if (ddlGender.SelectedValue == "male") isMale = true;
            else if (ddlGender.SelectedValue == "female") isMale = false;
            amc.updateAccountDetails(txtEmpId.Text, txtUserId.Value, txtUserName.Text, txtFirstName.Text, txtLastName.Text, isMale, txtEmail.Text, ddlRole.SelectedValue, isApproved);
            Session.Remove("UserId_gettingDetail");
            Response.Redirect("Accounts.aspx");
        }

        protected void ResetPwd_Click(object sender, EventArgs e)
        {
            try
            {
                string newPwd = amc.resetPassword(txtUserId.Value);

                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Credentials = new System.Net.NetworkCredential("duthehao@gmail.com", "172177637640");
                //smtpClient.UseDefaultCredentials = true;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.EnableSsl = true;
                smtpClient.Host = "smtp.gmail.com";

                MailMessage mail = new MailMessage();
                ad = amc.getAccountDetails(txtUserId.Value);
                //Setting From , To and CC
                mail.From = new MailAddress("duthehao@gmail.com");
                mail.To.Add(new MailAddress(ad.Email));
                mail.Subject = "Password reset";
                mail.Body = "Dear "+ad.empFirstName+ " "+ad.empLastName+ "\n\nYour password has been reseted.\n\nYour new password: " + newPwd;

                smtpClient.Send(mail);
            }
            catch (Exception ex)
            {
            }
        }
    }
}